package com.deparse.dpframe.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;

import java.util.List;

/**
 * Created by hsy on 2016/3/22.
 */
public class DPFloatingMenu extends PopupWindow {
    DPFullDrawListView mListView;
    private List<View> mMenuItems;
    private CustomAdapter adapter;
    private FrameLayout mContainer;

    public DPFloatingMenu(Context context) {
        super(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        init(context);
    }

    private void init(Context context) {
        mContainer = new FrameLayout(context){
            @Override
            public boolean dispatchKeyEvent(KeyEvent event) {
                return super.dispatchKeyEvent(event);
            }
        };
        this.mListView = new DPFullDrawListView(context);
        setContentView(mListView);

        adapter = new CustomAdapter();
        mListView.setAdapter(adapter);
    }

    public void setAdapter(ListAdapter adapter) {
        mListView.setAdapter(adapter);
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener listener) {
        mListView.setOnItemClickListener(listener);
    }

    public void setOnItemLongClickListener(AdapterView.OnItemLongClickListener listener) {
        mListView.setOnItemLongClickListener(listener);
    }

    public void setMenuItems(List<View> items) {
        this.mMenuItems = items;
        adapter.notifyDataSetChanged();
    }

    public ListView getListView() {
        return mListView;
    }

    private class CustomAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return null == mMenuItems ? 0 : mMenuItems.size();
        }

        @Override
        public Object getItem(int position) {
            return null == mMenuItems ? null : mMenuItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return mMenuItems.get(position);
        }
    }
}
